home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Chef 1.1 so Folder / Chef ƒ / MSG Shell ƒ / msg main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  6.6 KB  |  303 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg main.c
  4.  
  5. Purpose:    This module handles the event loop and event dispatching.
  6.  
  7.  
  8. Chef -=- convert text to Swedish chef talk
  9. Copyright ©1994, Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "msg main.h"
  29. #include "msg integrity.h"
  30. #include "msg graphics.h"
  31. #include "msg about.h"
  32. #include "msg help.h"
  33. #include "msg menus.h"
  34. #include "msg prefs.h"
  35. #include "msg environment.h"
  36. #include "chef.h"
  37. #include "chef progress.h"
  38. #include "AppleEvents.h"
  39. #include "EPPC.h"
  40.  
  41. void main(void)
  42. {
  43.     MaxApplZone();    
  44.     InitGraf(&thePort);
  45.     InitFonts();
  46.     FlushEvents(everyEvent, 0);
  47.     InitWindows();
  48.     InitMenus();
  49.     TEInit();
  50.     InitDialogs(0L);
  51.     InitCursor();
  52.     GetDateTime(&randSeed);
  53.     
  54.     CheckEnvironment();    
  55.     DoIntegrityCheck();    
  56.     InitMSGGraphics();
  57.     InitHelp();
  58.     PrefsError(PreferencesInit());
  59.     InitEnvironment();    
  60.     InitProgram();
  61.     EventLoop();    
  62.     ShutDownEnvironment();
  63.     ExitToShell();
  64. }
  65.  
  66. void EventLoop(void)
  67. {
  68.     EventRecord        theEvent;
  69.     int                i;
  70.     
  71.     while (!gDone)
  72.     {
  73.         SetCursor(&arrow);
  74.         HiliteMenu(0);
  75.         
  76.         if (FrontWindow()!=0L)
  77.         {
  78.             for (i=0; i<NUM_WINDOWS; i++)
  79.             {
  80.                 if (FrontWindow()==gTheWindow[i])
  81.                 {
  82.                     SetPort(gTheWindow[i]);
  83.                     i=NUM_WINDOWS;
  84.                 }
  85.             }
  86.         }
  87.             
  88.         if (WaitNextEvent(everyEvent, &theEvent, gIsInBackground ? 100 :
  89.             (((gTheWindow[kAboutMSG]!=0L) && (FrontWindow()==gTheWindow[kAboutMSG])) ?
  90.             0 : 30), 0L))
  91.             DispatchEvents(theEvent);
  92.         
  93.         if ((!gIsInBackground) && (gTheWindow[kAboutMSG]!=0L) &&
  94.             (FrontWindow()==gTheWindow[kAboutMSG]))
  95.             DoTheMSGThing();
  96.     }
  97. }
  98.  
  99. void DispatchEvents(EventRecord theEvent)
  100. {
  101.     int                i;
  102.     OSErr            isHuman;
  103.     Point            thisPoint;
  104.     int                index;
  105.     char            thisChar;
  106.     unsigned long    dummy;
  107.     WindowPtr        theWindow;
  108.     
  109.     index=-1;
  110.     if ((theWindow=FrontWindow())!=0L)
  111.     {
  112.         for (i=0; (i<NUM_WINDOWS) && (index==-1); i++)
  113.             if (theWindow==gTheWindow[i])
  114.                 index=i;
  115.     }
  116.     
  117.     switch (theEvent.what)
  118.     {
  119.         case nullEvent:
  120.             break;
  121.         case mouseDown:
  122.             HandleMouseDown(theEvent);
  123.             break;
  124.         case keyDown:
  125.         case autoKey:
  126.             thisChar=(char)(theEvent.message & charCodeMask);
  127.             if(theEvent.modifiers & cmdKey)
  128.             {
  129.                 AdjustMenus();
  130.                 HandleMenu(MenuKey(thisChar));
  131.             }
  132.             else
  133.                 switch (index)
  134.                 {
  135.                     case kAbout:
  136.                         CloseTheWindow(index);
  137.                         break;
  138.                     case kAboutMSG:
  139.                         UpdateTheWindow(index);
  140.                         Delay(30, &dummy);
  141.                         CloseTheWindow(index);
  142.                         break;
  143.                     case kHelp:
  144.                         HelpKeyEvent(thisChar);
  145.                         break;
  146.                 }
  147.             break;
  148.         case diskEvt:
  149.             if (HiWord(theEvent.message)!=noErr)
  150.             {
  151.                 DILoad();
  152.                 SetPt(&thisPoint, 120, 120);
  153.                 isHuman=DIBadMount(thisPoint, theEvent.message);
  154.                 DIUnload();
  155.             }
  156.             break;
  157.         case updateEvt:
  158.             BeginUpdate((WindowPtr)theEvent.message);
  159.             
  160.             for (i=0; i<NUM_WINDOWS; i++)
  161.                 if ((WindowPtr)theEvent.message == gTheWindow[i])
  162.                     UpdateTheWindow(i);
  163.                     
  164.             if ((theWindow!=0L) && (theWindow==gProgressDlog))
  165.                 UpdateDialog(theWindow, theWindow->visRgn);
  166.  
  167.             EndUpdate((WindowPtr)theEvent.message);
  168.             break;
  169.         case activateEvt:
  170.             if (((WindowPtr)theEvent.message==gTheWindow[kAboutMSG]) &&
  171.                 ((theEvent.modifiers&activeFlag)==0))
  172.                 UpdateTheWindow(kAboutMSG);
  173.             break;
  174.         case osEvt:
  175.             if (((theEvent.message>>24)&0x0FF)==suspendResumeMessage)
  176.             {
  177.                 gIsInBackground=((theEvent.message&resumeFlag)==0);
  178.                 if ((gIsInBackground) && ((gTheWindow[kAboutMSG]!=0L) &&
  179.                     (FrontWindow()==gTheWindow[kAboutMSG])))
  180.                     UpdateTheWindow(kAboutMSG);
  181.             }
  182.             break;
  183.         case kHighLevelEvent:
  184.             if (gHasAppleEvents)
  185.                 AEProcessAppleEvent(&theEvent);
  186.             break;
  187.     }
  188. }
  189.  
  190. void HandleMouseDown(EventRecord theEvent)
  191. {
  192.     WindowPtr            theWindow;
  193.     int                    windowCode;
  194.     long                windSize;
  195.     GrafPtr                oldPort;
  196.     int                    i;
  197.     Rect                sizeRect;
  198.     Boolean                gotone;
  199.     int                    index;
  200.     unsigned long        dummy;
  201.     
  202.     windowCode=FindWindow(theEvent.where, &theWindow);
  203.     index=-1;
  204.     for (i=0; (i<NUM_WINDOWS) && (index==-1); i++)
  205.         if (theWindow==gTheWindow[i])
  206.             index=i;
  207.     
  208.     switch (windowCode)
  209.     {
  210.         case inMenuBar:
  211.             AdjustMenus();
  212.             HandleMenu(MenuSelect(theEvent.where));
  213.             break;
  214.         case inContent:
  215.             if(FrontWindow() != theWindow)
  216.             {
  217.                 if (FrontWindow()==gTheWindow[kAboutMSG])
  218.                     UpdateTheWindow(kAboutMSG);
  219.                 SelectWindow(theWindow);
  220.             }
  221.             else
  222.                 switch (index)
  223.                 {
  224.                     case kAbout:
  225.                         CloseTheWindow(index);
  226.                         break;
  227.                     case kAboutMSG:
  228.                         UpdateTheWindow(index);
  229.                         Delay(30, &dummy);
  230.                         CloseTheWindow(index);
  231.                         break;
  232.                     case kHelp:
  233.                         HelpEvent();
  234.                         break;
  235.                 }
  236.             break;
  237.         case inSysWindow:
  238.             SystemClick(&theEvent, theWindow);
  239.             break;
  240.         case inDrag:
  241.             DragWindow(theWindow, theEvent.where, &gDragRect);
  242.             if (index>=0)
  243.                 gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  244.             break;
  245.         case inGoAway:
  246.             if (TrackGoAway(theWindow, theEvent.where))
  247.             {
  248.                 gotone=FALSE;
  249.                 for (i=0; (i<NUM_WINDOWS) && (!gotone); i++)
  250.                     gotone=(theWindow==gTheWindow[i]);
  251.                     
  252.                 if (gotone)
  253.                     CloseTheWindow(i-1);
  254.                 else
  255.                     DisposeWindow(theWindow);
  256.                 
  257.                 AdjustMenus();
  258.             }
  259.             break;
  260.         case inGrow:
  261.             sizeRect = screenBits.bounds;
  262.             OffsetRect(&sizeRect, sizeRect.left, sizeRect.top);
  263.             
  264.             windSize = GrowWindow(theWindow, theEvent.where, &sizeRect);
  265.             if(windSize != 0)
  266.             {
  267.                 GetPort(&oldPort);
  268.                 SetPort(theWindow);
  269.                 EraseRect(&theWindow->portRect);
  270.                 SizeWindow(theWindow, LoWord(windSize), HiWord(windSize), TRUE);
  271.                 InvalRect(&theWindow->portRect);
  272.                 SetPort(oldPort);
  273.             }
  274.             
  275.             if (index>=0)
  276.                 gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  277.             
  278.             break;
  279.         case inZoomIn:
  280.         case inZoomOut:
  281.             if(TrackBox(theWindow, theEvent.where, windowCode))
  282.             {
  283.                 GetPort(&oldPort);
  284.                 SetPort(theWindow);
  285.                 ZoomWindow(theWindow, windowCode, FALSE);
  286.                 InvalRect(&theWindow->portRect);
  287.                 SetPort(oldPort);
  288.             }
  289.             
  290.             if (index>=0)
  291.                 gWindowBounds[index] = (*(((WindowPeek)gTheWindow[index])->contRgn))->rgnBBox;
  292.             
  293.             break;
  294.     }
  295. }
  296.  
  297. void ShutDownEnvironment(void)
  298. {
  299.     ShutDownProgram();
  300.     ShutDownMSGGraphics();
  301.     ShutDownHelp();
  302. }
  303.